package com.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;

import com.entity.FileInfo;

import android.app.Activity;
import android.widget.Toast;

public class FileHelper {

	/** 获取一个文件夹下的所有文件 **/
	public static ArrayList<FileInfo> getFiles(Activity activity, String path) {
		File f = new File(path);
		File[] files = f.listFiles();
		if (files == null) {
			Toast.makeText(activity, "无相关文件", Toast.LENGTH_SHORT).show();
			return null;
		}

		ArrayList<FileInfo> fileList = new ArrayList<FileInfo>();
		// 获取文件列表
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			FileInfo fileInfo = new FileInfo();
			fileInfo.Name = file.getName();
			fileInfo.IsDirectory = file.isDirectory();
			fileInfo.Path = file.getPath();
			fileInfo.folderVideo = getFolder(file);
			fileInfo.motifyTime = file.lastModified();
			try {
				double size = getFileSize(file) / 1024.0f / 1024.0f;
				fileInfo.Size = size;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			fileList.add(fileInfo);
		}

		// 排序
		 Collections.sort(fileList, new FileComparator());

		return fileList;
	}
	
	/** 删除文件 **/
	public static void deleteFile(File f) {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			if (files != null && files.length > 0) {
				for (int i = 0; i < files.length; ++i) {
					deleteFile(files[i]);
				}
			}
		}
		f.delete();
	}

	public static String[] getFolder(File f){
		File flist[] = f.listFiles();
		String[] temp = new String[flist.length];
		for (int i = 0; i < flist.length; i++){
			temp[i] = flist[i].getPath();
		}
		return temp;
	}
	
	public static long getFileSize(File f) throws Exception// 取得文件夹大小
	{
		long size = 0;
		File flist[] = f.listFiles();
		for (int i = 0; i < flist.length; i++) {
			if (flist[i].isDirectory()) {
				size = size + getFileSize(flist[i]);
			} else {
				size = size + flist[i].length();
			}
		}
		return size;
	}
}
